import { Component, OnInit } from '@angular/core';
import { Course } from '../../model/course';
import { Student } from '../../model/Student';
import { Book } from '../../model/book';
import { CourseService } from '../../service/course.service';
import { CourseResult } from '../../model/courseresult';

@Component({
  selector: 'app-scheduling',
  templateUrl: './scheduling.component.html',
  styleUrls: ['./scheduling.component.css']
})
export class SchedulingComponent implements OnInit {

  cl:Array<Course>
  sl:Array<Student>
  bl:Array<Book>
  tosavecourse:CourseResult
  
  constructor(private courseservice:CourseService) { 
   this.tosavecourse = new CourseResult();

  }

  ngOnInit() {
    this.initCourse();
    
    this.initStudent();
    this.initBook();

  }


  initCourse(){
    this.cl = new Array();
    for(let i=0 ;i<25;i++){
     let c = new Course()
     c.id= i;
     this.cl.push(c);
 
 
    }
 
   }
 
 
   setCourse(searchresult:Array<CourseResult>){
    
  
    
 
    for(let x of searchresult){
    
      let t = new Course();
      t.classroom= x.classroom
      t.id=x.cid
      t.name= x.name
      t.student= x.student
      t.teacher= x.teacher
 
 
     this.cl[x.cid]=t;
 
 
    }
 
 
 
   }
 
 
 //初始化班级信息
   initStudent(){
   this.sl = new Array();
   for(let i=0; i<30;i++){
     let s =new Student();
     s.id="主键"+i;
     s.name = "班级"+i
  this.sl.push(s);
 
   }
   }
 
 
 
   initBook(){
     this.bl = new Array();
    this.courseservice.getBookList()
    .then((data:any)=>{
      this.bl= data;
    })
 
 
   }
 
   onStudentSelect(s:Student){
 
    console.dir(s);
 
   }
 
   onBookSelect(b:Book){
 
   console.dir(b);
    this.courseservice.getCourseListByParm('book',b.id)
    .then((data:any)=>{
 
    console.dir(data);
   this.setCourse(data);
 
 
    })
 
 
 
   }
 
   save(){

    console.dir(this.tosavecourse);
   



   }


   onGridSelect(cid:number){

    alert("你选中了"+cid)

    this.tosavecourse.cid = cid
   }


   onBookValueSelect(b:Book){

    this.tosavecourse.book = b.id
    this.tosavecourse.name = b.name
   }

}
